package com.jp.knowledge.activity;

import android.content.Intent;
import android.os.Build;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.canyinghao.canrefresh.CanRefreshLayout;
import com.google.gson.JsonObject;
import com.jiayen.util.ToasUtil;
import com.jp.knowledge.comm.SlidingActivity;
import com.jp.knowledge.comm.InfoDetailData;
import com.jp.knowledge.R;
import com.jp.knowledge.adapter.InfoTopicAdapter;
import com.jp.knowledge.dialog.InfoDetailCommentDialog;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.http.ToutiaoGetDataUtil;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.model.TopicListInfo;
import com.jp.knowledge.my.model.OrganizaModel;
import com.jp.knowledge.util.ResultSubscriber;
import com.jp.knowledge.view.TalkBottomBarView;

import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/4/10.
 */

public class InfoTopicListActivity extends SlidingActivity implements View.OnClickListener, CanRefreshLayout.OnRefreshListener, CanRefreshLayout.OnLoadMoreListener, ToutiaoGetDataUtil.GetDataCallback, TalkBottomBarView.CommentSuccessCallback {

    @ViewInject(R.id.topic_return)
    private ImageView returnBtn;
    @ViewInject(R.id.refresh)
    private CanRefreshLayout refreshLayout;
    @ViewInject(R.id.can_content_view)
    private RecyclerView topicLisView;
    @ViewInject(R.id.topic_remind_view)
    private LinearLayout remindView;
    @ViewInject(R.id.topic_shade_view)
    private View shadeView;
    @ViewInject(R.id.topic_bottom_talk_bar)
    private TalkBottomBarView bottomBarView;

    private InfoDetailCommentDialog commentDialog;

    private ToutiaoGetDataUtil getDataUtil;
    private InfoTopicAdapter infoTopicAdapter;

    @Override
    protected int getResourceId() {
        return R.layout.activity_info_topic_list;
    }

    @Override
    protected void init() {
        initData();
        initView();
        initDialog();
        refreshLayout.autoRefresh();
    }

    @Override
    protected void onResume() {
        super.onResume();
        updataTopicListView();
    }

    private void initData(){
        getDataUtil = new ToutiaoGetDataUtil(this);
        getDataUtil.setGetDataCallback(this);

        String uuid = application.getUserData() == null ? null : application.getUserData().getUuid();
        infoTopicAdapter = new InfoTopicAdapter(this, new ArrayList<TopicListInfo>(), uuid);
    }

    private void initView(){
        returnBtn.setOnClickListener(this);
        bottomBarView.setShadeView(shadeView);
        bottomBarView.setContentViewStyle(5);
        bottomBarView.setCommentSuccessCallback(this);

        topicLisView.setHasFixedSize(true);
        topicLisView.setLayoutManager(new LinearLayoutManager(this));
        topicLisView.setAdapter(infoTopicAdapter);

        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnLoadMoreListener(this);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(0x22000000);
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
            int statusBarHeight = 0;
            int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
            if (resourceId > 0)
                statusBarHeight = getResources().getDimensionPixelSize(resourceId);
            contentView.setPadding(contentView.getPaddingLeft(),
                    contentView.getPaddingTop() + statusBarHeight,
                    contentView.getPaddingRight(),
                    contentView.getPaddingBottom());
        }
    }

    private void initDialog(){

        commentDialog = new InfoDetailCommentDialog(mContext);
        commentDialog.setOnSendBtnClickListener(new InfoDetailCommentDialog.OnSendBtnClickListener() {
            @Override
            public void onSendClick() {
                String commentContent = commentDialog.getEditContent();
                if(commentContent.trim().length() == 0) {
                    ToasUtil.toast(mContext, "请输入有效内容后再提交");
                    return;
                }
                InfoDetailData infoDetailData = InfoDetailData.getInfoDetail();
                JsonObject param = new JsonObject();
                param.addProperty("content", commentContent);
                param.addProperty("type", infoDetailData.getLinkType());
                param.addProperty("infoId", infoDetailData.getLinkId());
                HttpHelper.getInstance(mContext).addComment(param, 0x01, new ResultSubscriber.OnResultListener() {
                    @Override
                    public void onStart(int requestType) {
                        loading.show();
                    }
                    @Override
                    public void onCompleted(int requestType) {
                        loading.cancel();
                    }
                    @Override
                    public void onError(int requestType) {
                        onCompleted(requestType);
                    }
                    @Override
                    public void onNext(IModel data, int requestType) {
                        if(data.getErrcode() != 0){
                            ToasUtil.toast(mContext, data.getMessage());
                            onError(requestType);
                        }
                        TopicListInfo topicListInfo = data.getEntity(TopicListInfo.class);
                        commentSuccess(topicListInfo);
                        commentDialog.clearEditContent();
                    }
                });
                commentDialog.dismiss();
            }
        });
        bottomBarView.setOnCommentBtnClickListener(new TalkBottomBarView.OnCommentBtnClickListener() {
            @Override
            public void onCommentBtnClick(View v) {
                commentDialog.show();
            }
        });

        bottomBarView.setOnShareBtnClickListener(new TalkBottomBarView.OnShareBtnClickListener() {
            @Override
            public void onShareBtnClick(View shareBtn) {
                InfoDetailData infoDetailData = InfoDetailData.getInfoDetail();
                Intent intent = new Intent(mContext, TeamCircleCreateActivity.class);
                intent.putExtra(TeamCircleCreateActivity.INTENT_TYPE, 3);
                intent.putExtra(TeamCircleCreateActivity.INTENT_LEVEL, 1);
                intent.putExtra(TeamCircleCreateActivity.INTENT_LINK_ICON, infoDetailData.getLinkIcon());
                intent.putExtra(TeamCircleCreateActivity.INTENT_LINK_ID, infoDetailData.getLinkId());
                intent.putExtra(TeamCircleCreateActivity.INTENT_LINK_TITLE, infoDetailData.getLinkTitle());
                intent.putExtra(TeamCircleCreateActivity.INTENT_LINKK_TYPE, infoDetailData.getLinkType());
                try {
                    OrganizaModel organizaModel = application.getUserData().getCompanyList().get(0);
                    intent.putExtra(TeamCircleCreateActivity.COMPANY_ID, organizaModel.getCompanyId());
                    intent.putExtra(TeamCircleCreateActivity.COMPANY_TYPE, organizaModel.getCompanyType());
                    intent.putExtra(TeamCircleCreateActivity.COMPANY_NAME, organizaModel.getCompanyName());
                } catch (Exception e){} finally {
                    mContext.startActivity(intent);
                }
            }
        });
    }

    private void updataTopicListView(){
        List<TopicListInfo> topicListInfoList = InfoDetailData.getInfoDetail().getTopicList();
        if(topicListInfoList.size() == 0)
            remindView.setVisibility(View.VISIBLE);
        else
            remindView.setVisibility(View.GONE);
        infoTopicAdapter.updateAdapter(InfoDetailData.getInfoDetail().getTopicList());
    }

//    @Override
//    public void finish() {
//        super.finish();
//        overridePendingTransition(R.anim.topic_activity_anim_enter,
//                R.anim.topic_activity_anim_exit);
//    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.topic_return:
                finish();
                break;
        }
    }

    @Override
    public void onRefresh() {
        if(!getDataUtil.getTopicList(0))
            refreshLayout.refreshComplete();
    }

    @Override
    public void onLoadMore() {
        if(!getDataUtil.getTopicList(1))
            refreshLayout.loadMoreComplete();
    }

    @Override
    public void onFinish(int requestCode, boolean isSuccess) {
        if(requestCode == ToutiaoGetDataUtil.GET_TOPIC_LIST_REFRESH){    // 下拉刷新
            refreshLayout.refreshComplete();
            updataTopicListView();
        } else if(requestCode == ToutiaoGetDataUtil.GET_TOPIC_LIST_LOAD){       // 加载更多
            refreshLayout.loadMoreComplete();
            updataTopicListView();
        }
    }

    @Override
    public void commentSuccess(TopicListInfo data) {
        if(data == null)
            return;
        List<TopicListInfo> topicListInfoList = InfoDetailData.getInfoDetail().getTopicList();
        topicListInfoList.add(0, data);
        updataTopicListView();
        topicLisView.smoothScrollToPosition(0);
    }
}
